package en.edu.zxj.rabbit.extensions.demo.controller;

import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-09-21
 * @time: 21:43:22
 */
@RestController
@RequestMapping("/trans")
public class TransactionProducer {
    @Resource(name = "transRabbitTemplate")
    private RabbitTemplate rabbitTemplate;

    @Transactional // 开启事务, 如果方法执行成功, 则两条消息都会发送成功, 如果方法执行失败, 则两条消息都不会发送; 注释这个注解, 会发现第一条消息发送成功, 第二条消息发送失败
    @RequestMapping("/send")
    public String send() {
        rabbitTemplate.convertAndSend("","transactionQueue", "transaction test 1...");
        // int i = 1 / 0;
        rabbitTemplate.convertAndSend("","transactionQueue", "transaction test 2...");
        return "send";
    }
}
